/**
 * @file    voltronic_rs485.c
 * @brief   逆变器协议-日月元-RS485协议
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-04-30
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-30 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "voltronic_rs485.h"
#include "modbus_app.h"
#include "if_di.h"
#include "cfg_prj.h"
#include "sample.h"
#include "parallel_com.h"
#include "var_mng.h"
#include "param_mng.h"
/* macro/define --------------------------------------------------------------*/

/* typedef -------------------------------------------------------------------*/
#define INTPUT_REG_SIZE      (0xB3)
#define CMD_READ_IN_REG      (0x03)
/* local functions -----------------------------------------------------------*/
static void voltronoic_read_input_reg(md_data_type md_data_msg , uint8_t *tx_buf ,uint16_t *tx_len);
static void voltronoic_update_input_buf(void);
/* static variables ----------------------------------------------------------*/
static uint16_t input_reg_buf[INTPUT_REG_SIZE] = {0};

/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
uint8_t voltronoic_rs485_analysis(uint8_t *rx_buf , uint16_t rx_len ,uint8_t *tx_buf , uint16_t *tx_len)
{
    md_data_type recv_md_data = {0};
    if(TRUE == modbus_rtu_analysis(rx_buf , rx_len , &recv_md_data))
    {
        if(1 == recv_md_data.addr)
        {
            if((recv_md_data.reg_addr + recv_md_data.data_len <= INTPUT_REG_SIZE)
              &&(CMD_READ_IN_REG == recv_md_data.cmd_code))
            {
                voltronoic_update_input_buf();
                voltronoic_read_input_reg(recv_md_data , tx_buf , tx_len);
                return TRUE;
            }
            return FALSE;
        }
        return FALSE;
    }
    return FALSE;
}

/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
static void voltronoic_read_input_reg(md_data_type md_data_msg, uint8_t *tx_buf, uint16_t *tx_len) 
{
    uint16_t data_len = 0;
    uint16_t crc_code = 0;
    uint8_t i = 0;
    uint8_t *p_tx = tx_buf;
	uint16_t idx_cnt = 0;

    p_tx[idx_cnt ++] = md_data_msg.addr;
    p_tx[idx_cnt ++] = md_data_msg.cmd_code;
    data_len = md_data_msg.data_len;
    p_tx[idx_cnt ++] = data_len >> 8; 
    p_tx[idx_cnt ++] = data_len & 0xff;
    
    for (i = 0; i < md_data_msg.data_len; ++i) 
	{
        p_tx[idx_cnt ++] = input_reg_buf[i + md_data_msg.reg_addr] >> 8;
		p_tx[idx_cnt ++] = input_reg_buf[i + md_data_msg.reg_addr];
    }

    crc_code = modbus_calc_crc(tx_buf, idx_cnt);

    p_tx[idx_cnt ++] = crc_code & 0xff;
    p_tx[idx_cnt ++] = crc_code >> 8;

    *tx_len = idx_cnt;   //修改
}

static void voltronoic_update_input_buf(void)
{
    int32_t sys_curr = 0;
    uint8_t i = 0;
    uint32_t v32_val = 0;

    input_reg_buf[0x00] = 0;
    input_reg_buf[0x01] = 0x0A;
    input_reg_buf[0x02] = 0x03;
	input_reg_buf[0x03] = 0;
    input_reg_buf[0x04] = (SOFT_VER_MAIN << 8) + SOFT_VER_RELEASE;
    input_reg_buf[0x05] = 0;
    input_reg_buf[0x06] = var_get_data(VAR_ID_HARD_VER);

    input_reg_buf[0x10] = param_get_data(PAR_ID_CB_CELL_NUM);
    for(i = 0 ;i < 20; ++ i)
    {
        if(i < input_reg_buf[0x10])
            input_reg_buf[i + 0x11] = master_data_reg.avg_cell_buf[i]*0.01;
        else
            input_reg_buf[i + 0x11] = 0;
    }
    input_reg_buf[0x25] = param_get_data(PAR_ID_CB_TEMP_NUM);	
    for(i = 0 ;i < 10; ++ i)
    {
        if(i < input_reg_buf[0x25])
            input_reg_buf[0x26 + i ] = var_get_data(VAR_ID_CELL_TEMP01 + i) + 2231;
        else
            input_reg_buf[0x26 + i ] = 0;
    }
    if(master_data_reg.total_bus_curr & 0x80000000)
    {
        sys_curr = (~master_data_reg.total_bus_curr) + 1;
        input_reg_buf[0x30] = 0;
        input_reg_buf[0x31] = sys_curr * 0.1;
    }
    else
    {
        input_reg_buf[0x30] = master_data_reg.total_bus_curr * 0.1;
        input_reg_buf[0x31] = 0;
    }

    input_reg_buf[0x32] = master_data_reg.avg_batt_volt * 0.1;
    input_reg_buf[0x33] = master_data_reg.avg_soc;
	v32_val = master_data_reg.total_full_cap * 10;
    input_reg_buf[0x34] = v32_val >> 16;
    input_reg_buf[0x35] = v32_val;
    
    input_reg_buf[0x40] = param_get_data(PAR_ID_CB_CELL_NUM);
    input_reg_buf[0x41] = 0x00;
    input_reg_buf[0x42] = 0x00;
    input_reg_buf[0x43] = 0x00;
    input_reg_buf[0x44] = 0x00;
    input_reg_buf[0x45] = 0x00;
    input_reg_buf[0x46] = 0x00;
    input_reg_buf[0x47] = 0x00;
    input_reg_buf[0x48] = 0x00;
    input_reg_buf[0x49] = 0x00;
    input_reg_buf[0x4A] = 0x00;
    input_reg_buf[0x50] = param_get_data(PAR_ID_CB_TEMP_NUM);	
    input_reg_buf[0x51] = 0x00;
    input_reg_buf[0x52] = 0x00;
    input_reg_buf[0x53] = 0x00;
    input_reg_buf[0x54] = 0x00;
    input_reg_buf[0x55] = 0x00;

    input_reg_buf[0x61] = 0x00;
    input_reg_buf[0x62] = 0x00;
    input_reg_buf[0x63] = 0x00;
	
    if(master_data_reg.total_prp_msg.bit.occ)
        input_reg_buf[0x64] = 0x02;
    else
        input_reg_buf[0x64] = 0x00;

    if(master_data_reg.total_prp_msg.bit.ocd)
        input_reg_buf[0x65] = 0x02;
    else
        input_reg_buf[0x65] = 0x00;

    if(master_data_reg.total_prp_msg.bit.otc)
        input_reg_buf[0x66] = 0x02;
    else if(master_data_reg.total_prp_msg.bit.utc)
        input_reg_buf[0x66] = 0x01;
    else
        input_reg_buf[0x66] = 0x00;

    if(master_data_reg.total_prp_msg.bit.otd)
        input_reg_buf[0x67] = 0x02;
    else if(master_data_reg.total_prp_msg.bit.utd)
        input_reg_buf[0x67] = 0x01;
    else
        input_reg_buf[0x67] = 0x00;

    input_reg_buf[0x68] = 0x00;
    input_reg_buf[0x69] = 0x00;

    input_reg_buf[0x71] = var_get_data(VAR_ID_TP_SOP_DV);
    if(var_get_data(VAR_ID_TP_SOP_CC) > 0)
	{
		input_reg_buf[0x70] = var_get_data(VAR_ID_TP_SOP_CV);
		input_reg_buf[0x72] = var_get_data(VAR_ID_TP_SOP_CC);
		
	}
	else
	{
		input_reg_buf[0x70] = 33.75 * param_get_data(PAR_ID_CB_CELL_NUM);
		input_reg_buf[0x72] = 10;
	}
	
	input_reg_buf[0x73] = var_get_data(VAR_ID_TP_SOP_DC);
    input_reg_buf[0x74] = 0;
	
    if(master_data_reg.fet_status & 0x02)
        input_reg_buf[0x74] |= 0x80;
    if(master_data_reg.fet_status & 0x01)
        input_reg_buf[0x74] |= 0x40;   
	
	if(parallel_get_pcs_ctrl(PARALLEL_PCS_FROCE_CHG1))
		input_reg_buf[0x74] |= 0x20;
	if(parallel_get_pcs_ctrl(PARALLEL_PCS_FROCE_CHG2))
		input_reg_buf[0x74] |= 0x10;
	if(var_get_data(VAR_ID_ASK_FULL_CHG))
        input_reg_buf[0x74] |= 0x08;
}

/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/
